package com.lujianfei.powersearch.ui.preview

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.view.WindowManager
import androidx.databinding.Observable
import androidx.databinding.Observable.OnPropertyChangedCallback
import com.gyf.immersionbar.ImmersionBar
import com.lujianfei.powersearch.R
import com.lujianfei.powersearch.base.BaseActivity
import com.lujianfei.powersearch.databinding.ActivityFullscreenPreviewBinding
import com.lujianfei.powersearch.ui.dialog.BottomDialog
import com.lujianfei.powersearch.ui.preview.viewmodel.FullScreenPreviewViewModel
import com.lujianfei.powersearch.utils.ToastUtils

class FullScreenPreviewActivity: BaseActivity<ActivityFullscreenPreviewBinding>() {

    companion object {
        fun startPreview(context: Context, images:String, currentIndex:Int = 0) {
            context.startActivity(Intent(context, FullScreenPreviewActivity::class.java)
                .putExtra("images",images)
                .putExtra("currentIndex",currentIndex)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
        }
    }

    private val mFullScreenPreviewViewModel: FullScreenPreviewViewModel by lazy { buildViewModel() }

    override fun useStatusBarUtils(): Boolean {
        return false
    }

    private fun updateStatusView() {
        val lp = binding?.statusView?.layoutParams
        if (lp != null) {
            lp.height = ImmersionBar.getStatusBarHeight(this)
            binding?.statusView?.layoutParams = lp
        }
    }

    override fun resourceId() = R.layout.activity_fullscreen_preview
    override fun initView() {
        binding?.vm = mFullScreenPreviewViewModel
        mFullScreenPreviewViewModel.startY = -ImmersionBar.getStatusBarHeight(this).toFloat()
        mFullScreenPreviewViewModel.endY = 0f
        window?.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        updateStatusView()
        ImmersionBar.with(this).init()
        parseIntent()
    }
    override fun initData() {
    }

    override fun initEvent() {
        mFullScreenPreviewViewModel.showIndicator.addOnPropertyChangedCallback(object :
            OnPropertyChangedCallback() {
            override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
                if (mFullScreenPreviewViewModel.showIndicator.get() == true) {
                    window?.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
                } else {
                    window?.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
                }
            }
        })
        mFullScreenPreviewViewModel.onVideoClickCallback = {
            startVideoPlayer(this, it)
        }
        mFullScreenPreviewViewModel.onLongClickListenerCallback = {
            showBottomDialog(it)
        }
    }

    private fun parseIntent() {
        val images = intent?.getStringExtra("images")
        val currentIndex = intent?.getIntExtra("currentIndex", 0)?:0
        mFullScreenPreviewViewModel.loadData(images, currentIndex)
    }

    private fun showBottomDialog(uri:String) {
        val mBottomDialog = BottomDialog()
        mBottomDialog.addItem(BottomDialog.ItemData(label = "使用系统图库查看") {
            it.dismiss()
            startActivity(Intent(Intent.ACTION_VIEW).setDataAndType(Uri.parse(uri), "image/*").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION))
        })
        mBottomDialog.addItem(BottomDialog.ItemData(label = "保存") {
            it.dismiss()
            mFullScreenPreviewViewModel.savePhoto(uri) {
                ToastUtils.show("保存成功")
            }
        })
        mBottomDialog.addItem(BottomDialog.ItemData(label = "取消") {
            it.dismiss()
        })
        mBottomDialog.showNow(supportFragmentManager, "mBottomDialog")
    }

    private fun startVideoPlayer(context:Context, imagePath: String) {
        context.startActivity(Intent(Intent.ACTION_VIEW).setDataAndType(Uri.parse(imagePath), "video/*").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
    }
}